<?php
/**
 * 单店审核日志模型
 * @author Administrator
 *  type:日志类型(
 *      11:专营店营销基金预算审核，12:专营店有效基金结算审核日志,
 *      21:区域营销基金预算审核,22:区域营销基金结算审核,
 *      31:车展基金预算审核，32：车展基金结算审核,
 *      41:巡展基金预算审核,42:巡展基金结算审核,
 *      51:专项基金项目审核,52:专项基金结算审核,
 *      )
 *  targetId:审核项Id
 */
class auditLogModel extends MY_Model{
    function auditLogModel(){
        parent::MY_Model();
        $this->table = 'AuditLog';
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //固定传播基金项目审核状态值
            if ($v['type'] == 51){
                $auditRs = $this->config->item('spreadProjectAuditRs');
            }
            //角色
            $list[$k]['auditRoleStr'] = isset($roleList[$v['auditRole']]) ? $roleList[$v['auditRole']] : '';
            //审核结果
            $list[$k]['auditRsStr'] = isset($auditRs[$v['auditRs']]) ? $auditRs[$v['auditRs']] : '';   
        }
        return $list;
    }
    /**
     * 添加审核日志
     * @param int $type 审核类型
     * @param array $data 审核信息
     * @param array $user 用户信息
     * @return bool
     */
    function addLog($type,$data = array(), $user = array()){
        $data['type'] = $type;
        $data['targetId'] || showError('请输入目标审核ID');
        $data['auditRole'] || showError('请选择用户角色');
        $data['auditUid'] || showError('请输入审核用户ID');
        $data['auditRs'] || showError('请输入审核结果');
        isset($data['auditDes']) || showError('请设置审核备注');
        $data['createTime'] = time();
        return $this->add($data);
    }
    /**
     * 获取审核日志列表
     * @param int $type
     * @param int $targetId
     * @return Ambigous <array, string, unknown>
     */
    function getLogs($type, $targetId){
        $findList = $this->getNewList(array('procId'=>$targetId, 'type'=>$type), null, null, '', 'id desc');
        return $findList;
    }
    /**
     * 获取最后一条审核列表
     * @param int $type
     * @param int $targetId
     * @return mixed
     */
    function getLastLog($type, $targetId){
        $findList = $this->getLogs($type, $targetId);
        $list = array_values($findList);
        return isset($list[0]) ? $list[0] : array();
    }
}